<?php

   //接收url参数
   $licesneID=$_GET['licesneID'];
   $keyword=$_GET['keyword'];
   $url=$_GET['url'];
   $maxid=$_GET['maxid'];
   //连接数据库
   $connect=mysql_connect("localhost","root","");
   mysql_select_db("news",$connect);
   $arr=array();
   $newsid=array();
   if($licesneID!=null){
	    $sql="select * from shouzhong where licesneid='{$licesneID}'";
		$result=mysql_query($sql);
		$row=mysql_fetch_assoc($result);
		$id=$row['id'];
		
		if($id!=""){ //存在licesneid
		      
			  //新闻过滤(得到过滤的sql语句)
			  if($keyword==""){  //没有任何过滤条件
				 
				 if($url==""){ 
					 
					 $sql="select * from news";
					 
				 }else{   //有url的过滤条件
				     
					 $web_array=array();
					 $web_array=explode(",",$url);
					 $web_array2=array();
					 foreach($web_array as $wangzhan){

						     $sql="select * from wangzhan where name like '%{$wangzhan}%'";
							 $res=mysql_query($sql);
							 $row=mysql_fetch_assoc($res);
							 array_push($web_array2,$row['id']);
						  
					 }
					 
					 //过滤新闻中的内容web
					 $strw1=" web=$web_array2[0]";
					 
					 for($i=1;$i<count($web_array);$i++){
						 
			             $strw2.=" or web=$web_array2[$i]";
			         } 
			         $rel_web=$strw1.$strw2;
					
					 $sql="select * from news where".$rel_web;
					
				 }
				  
			  }else{  //有keyword的过滤条件
				  
				   //标题和内容
				   $title_array=array();
				   $title_array=explode(",",$keyword);
				   $strt1=" (title like '%{$title_array[0]}%' or content like '%{$title_array[0]}%'";
				   for($i=1;$i<count($title_array);$i++){
					   
						 $strt2.=" or title like '%{$title_array[$i]}%' or content like '%{$title_array[$i]}%'";
						 
				   } 
				   $rel_title=$strt1.$strt2.")";
				   if($url==""){ 
					 
						$sql="select * from news where".$rel_title;
						
					 
				   }else{   //有url的过滤条件
				   
					    $web_array=array();
					    $web_array=explode(",",$url);
					    $web_array2=array();
					    foreach($web_array as $wangzhan){
							
						     $sql="select * from wangzhan where name like '%{$wangzhan}%'";
							 $res=mysql_query($sql);
							 $row=mysql_fetch_assoc($res);
							 array_push($web_array2,$row['id']);
						  
					    }
						
					    //过滤新闻中的内容web
					    $strw1=" and(web=$web_array2[0]";
					    for($i=1;$i<count($web_array);$i++){
			              $strw2.=" or web=$web_array2[$i]";
			            } 
			              $rel_web=$strw1.$strw2.")";
					      $sql="select * from news where".$rel_title.$rel_web;
						  
				   }
				   

			    }
				
				//条件查询的sql语句已经得到
				$result=mysql_query($sql);
				while($row=mysql_fetch_array($result)){
					  $nid=$row['id']; 
					  
					  //得到网站的名字
					  $web_id=$row4['web'];
					  $sql_web="select * from wangzhan where id=$web_id";
					  $result_web=mysql_query($sql_web);
			          $row_web=mysql_fetch_assoc($result_web);
			          $web2=$row_web['name'];
					  
					  if($nid!=""){
						 //是否有重复的id
						 if($nid>$maxid){  //没有重复的,可以发送
						    
							$input = array("id"=>$row['id'], "title"=>$row['title'],"web"=>$web2,"author"=>$row['author'],"publishtime"=>$row['publishtime'],"content"=>$row4['content'],"link"=>$row['link']);
							
							array_push($arr,$input);
							array_push($newsid,$nid);  //把最大的id传过去
							
							
						 }
						  
						  
					  }
					  
					
				}
				
				
				$max_newsid=max($newsid);
				//传出的值
				$array["maxid"]=$max_newsid;
				$array["newslist"]=$arr;
				echo json_encode($array); 
				
				
		}else{
			//把licesneID加入到数据库中
			//备注存的是ip
			$ip=$_SERVER['REMOTE_ADDR'];
			$sql="insert into shouzhong(name,licesneid,beizhu) value('$ip','$licesneID','$ip')";
			//mysql_query($sql);
			echo "已添加licesneID,请重新访问!";
	   
		}
		
		
		
   }
   

   
?>